package com.juno.cafenoa.menu;

import java.util.ArrayList; 

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.juno.cafenoa.R;
import com.juno.cafenoa.TopNavigatorView;
import com.juno.cafenoa.sqlite.CafeNoaDBAdapter;
import com.juno.ui.uitableview.UITableView;
import com.juno.ui.uitableview.UITableView.ClickListener;

public class Category extends Activity implements OnTouchListener{
	Context context;
	Activity activity;
	private TopNavigatorView navi;
	private Menu menuDetailList;
	private CategoryListView categoryListView;

	private ViewFlipper mViewFlipper;
	
	private CafeNoaDBAdapter categoryDB = null;
	ArrayList<CategoryBasicItem> categoryList;
	private int ViewPosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_order);
		navi = (TopNavigatorView) findViewById(R.id.navi_order);
		navi.init("Order");
		
		mViewFlipper = (ViewFlipper) findViewById(R.id.order_viewflipper);
		mViewFlipper.setOnTouchListener(this);
		categoryListView = new CategoryListView(this);
		mViewFlipper.addView(categoryListView);
	}

	private void MoveNextView() {
		ViewPosition = 1;
		mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_right));
		mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_left));
		mViewFlipper.showNext();
	}

	private void MovePreviousView() {
		ViewPosition = 0;
		mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_left));
		mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_right));
		mViewFlipper.showPrevious();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		// Cart에서 삭제되었는지 Refresh를해 CheckBox에 적용
		if(menuDetailList != null)
			menuDetailList.refreshCheckBox();
		Log.v("onResume","onResume");
		
	}
	
	
	
	@Override
	protected void onDestroy() {
		if(categoryDB != null)
			categoryDB.Close();
		super.onDestroy();
	}



	int mPreTouchPosX = 0;
	
	@Override
	public boolean onTouch(View v, MotionEvent ev) {
		if(ev.getAction()==MotionEvent.ACTION_DOWN){
			mPreTouchPosX = (int)ev.getX();
		}
		if(ev.getAction()==MotionEvent.ACTION_UP){
			int nTouchPosX = (int) ev.getX();
			if(nTouchPosX>mPreTouchPosX){
				categoryListView.orderItem.clear();
				menuDetailList.clearTableView();
				MovePreviousView();
			}
			mPreTouchPosX = nTouchPosX;
		}
		return true;
	}
	
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(event.getAction() == KeyEvent.ACTION_DOWN){
			if(keyCode== KeyEvent.KEYCODE_BACK){
				if(ViewPosition == 1){
					MovePreviousView();
					return true;
				}
			}
		}
		return false;
	}



	public class CategoryListView extends LinearLayout {
		private Context mContext;
		private ArrayList<String> orderItem;
		private UITableView mTableView;

		public CategoryListView(Context context) {
			super(context);
			this.mContext = context;
			init();
		}

		public CategoryListView(Context context, AttributeSet attrs) {
			super(context, attrs);
			this.mContext = context;
			init();
		}

		private void init() {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			inflater.inflate(R.layout.order_view, this);
			mTableView = (UITableView) findViewById(R.id.orderTableView);
			CustomClickListener listener = new CustomClickListener();
			mTableView.setClickListener(listener);
			orderItem = new ArrayList<String>(); 
			
			// Category 추가
			categoryDB = new CafeNoaDBAdapter();
			categoryDB.open(mContext);
			categoryList = categoryDB.selectCategoryData();
			for(int i = 0; i < categoryList.size(); i++)
			{
				mTableView.addBasicItem(categoryList.get(i));
			}
			mTableView.commit();
		}

		protected UITableView getUITableView() {
			return mTableView;
		}

		private class CustomClickListener implements ClickListener {
			@Override
			public void onClick(final int index) {

				if(mViewFlipper.getChildCount()!=1)
				{
					mViewFlipper.removeViewAt(1);
				} 
				
				if(context == null)
					Log.d("Category.java", "Context is null");
				Log.d("Category.java", "categoryList.get(index).getId() = "+categoryList.get(index).getId());
				menuDetailList = new Menu(context, categoryList.get(index).getId());

				mViewFlipper.addView(menuDetailList);
				MoveNextView();
			}
		}
	}


}
